package com.yu.rpc.core.bootstrap;

import com.yu.rpc.core.RpcApplication;
import com.yu.rpc.core.config.RegistryConfig;
import com.yu.rpc.core.config.RpcConfig;
import com.yu.rpc.core.model.ServiceMetaInfo;
import com.yu.rpc.core.model.ServiceRegisterInfo;
import com.yu.rpc.core.registry.LocalRegistry;
import com.yu.rpc.core.registry.Registry;
import com.yu.rpc.core.registry.RegistryFactory;
import com.yu.rpc.core.server.HttpServer;
import com.yu.rpc.core.server.tcp.VertxTcpServer;

import java.util.List;

/**
 * 服务提供者初始化类
 */
public class ProviderBootStrap {
    public static void init(List<ServiceRegisterInfo<?>> serviceRegisterInfoList) {
        RpcApplication.init();
        RpcConfig rpcConfig = RpcApplication.getConfig();
        for (ServiceRegisterInfo<?> serviceRegisterInfo : serviceRegisterInfoList) {
            String serviceName = serviceRegisterInfo.getServiceName();
            //本地注册中心
            LocalRegistry.register(serviceName, serviceRegisterInfo.getImplClass());
            //注册中心
            RegistryConfig registryConfig = rpcConfig.getRegistryConfig();
            Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
            ServiceMetaInfo metaInfo = new ServiceMetaInfo(serviceName, rpcConfig.getServerHost(), rpcConfig.getServerPort());
            try {
                registry.register(metaInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        HttpServer server = new VertxTcpServer();
        server.doStart(RpcApplication.getConfig().getServerPort());
    }
}
